﻿using LiteDB;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using Mirai.Net.Api.ApplicationModel;
using Mirai.Net.Api.Model;
using Mirai.Net.Core;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Mirai.Net.Api.Controllers
{
    [ApiController]
    [Route("api/[controller]")]
    public class RobotController : ControllerBase
    {
        private readonly MiraiClient miraiClient;
        private readonly MiraiDbContext miraiDbContext;

        public RobotController(MiraiClient miraiClient, MiraiDbContext miraiDbContext)
        {
            this.miraiClient = miraiClient;
            this.miraiDbContext = miraiDbContext;
        }

        [HttpGet("ready")]
        public async Task<IActionResult> ReadyAsync()
        {
            try
            {
                await miraiClient.ConnectAsync();
                return Ok();
            }
            catch (Exception ex)
            {
                return BadRequest(ex.ToString());
            }
        }

        [HttpGet("add/{qq}")]
        public async Task AddAsync([FromRoute] string qq)
        {
            await miraiClient.ConnectAsync();
            await miraiClient.VerifyAsync(qq);
            miraiDbContext.Robots.Add(new Robot()
            {
                QQ = qq
            });
            await miraiDbContext.SaveChangesAsync();
        }

        [HttpGet("send/{qq}/{message}")]
        public async Task SendFriendMessageAsync([FromRoute] string qq, [FromRoute] string message)
        {
            await miraiClient.ConnectAsync();
            var robot = await miraiDbContext.Robots.FirstOrDefaultAsync();
            await miraiClient.VerifyAsync(robot.QQ);
            await miraiClient.SendFriendMessageAsync(qq, message);
        }

        [HttpPost("send/{qq}")]
        public async Task SendFriendMessageAsync([FromRoute] string qq, [FromBody] MultiLineMessage message)
        {
            await miraiClient.ConnectAsync();
            var robot = await miraiDbContext.Robots.FirstOrDefaultAsync();
            await miraiClient.VerifyAsync(robot.QQ);
            await miraiClient.SendFriendMessageAsync(qq, message.Message);
        }

        [HttpGet("sendGroup/{group}/{message}")]
        public async Task SendGroupMessageAsync([FromRoute] string group, [FromRoute] string message)
        {
            await miraiClient.ConnectAsync();
            var robot = await miraiDbContext.Robots.FirstOrDefaultAsync();
            await miraiClient.VerifyAsync(robot.QQ);
            await miraiClient.SendGroupMessageAsync(group, message);
        }


        [HttpPost("sendGroup/{group}")]
        public async Task SendGroupMessageAsync([FromRoute] string group, [FromBody] MultiLineMessage message)
        {
            await miraiClient.ConnectAsync();
            var robot = await miraiDbContext.Robots.FirstOrDefaultAsync();
            await miraiClient.VerifyAsync(robot.QQ);
            await miraiClient.SendGroupMessageAsync(group, message.Message);
        }

        [HttpGet("list")]
        public async Task<object> GetListAsync()
        {
            var robots = await miraiDbContext.Robots.ToListAsync();
            return new
            {
                items = robots.ToList(),
                total = robots.Count()
            };
        }
    }
}
